package cn.iocoder.yudao.module.vos.controller.admin.ecdr;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vos.controller.admin.ecdr.vo.*;
import cn.iocoder.yudao.module.vos.dal.dataobject.ecdr.ECdrDO;
import cn.iocoder.yudao.module.vos.service.ecdr.admin.ECdrService;

@Tag(name = "管理后台 - CDR")
@RestController
@RequestMapping("/vos/E-cdr")
@Validated
@TenantIgnore
public class ECdrController {

    @Resource
    private ECdrService eCdrService;

    @PostMapping("/create")
    @Operation(summary = "创建CDR")
    @PreAuthorize("@ss.hasPermission('vos:E-cdr:create')")
    public CommonResult<Long> createECdr(@Valid @RequestBody ECdrSaveReqVO createReqVO) {
        return success(eCdrService.createECdr(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新CDR")
    @PreAuthorize("@ss.hasPermission('vos:E-cdr:update')")
    public CommonResult<Boolean> updateECdr(@Valid @RequestBody ECdrSaveReqVO updateReqVO) {
        eCdrService.updateECdr(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除CDR")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vos:E-cdr:delete')")
    public CommonResult<Boolean> deleteECdr(@RequestParam("id") Long id) {
        eCdrService.deleteECdr(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除CDR")
                @PreAuthorize("@ss.hasPermission('vos:E-cdr:delete')")
    public CommonResult<Boolean> deleteECdrList(@RequestParam("ids") List<Long> ids) {
        eCdrService.deleteECdrListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得CDR")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vos:E-cdr:query')")
    public CommonResult<ECdrRespVO> getECdr(@RequestParam("id") Long id) {
        ECdrDO eCdr = eCdrService.getECdr(id);
        return success(BeanUtils.toBean(eCdr, ECdrRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得CDR分页")
    @PreAuthorize("@ss.hasPermission('vos:E-cdr:query')")
    public CommonResult<PageResult<ECdrRespVO>> getECdrPage(@Valid ECdrPageReqVO pageReqVO) {
        PageResult<ECdrDO> pageResult = eCdrService.getECdrPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ECdrRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出CDR Excel")
    @PreAuthorize("@ss.hasPermission('vos:E-cdr:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportECdrExcel(@Valid ECdrPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        // 直接调用 service 的“无分页导出列表”，内部会做 phoneTag 判定
        List<ECdrDO> list = eCdrService.getECdrListForExport(pageReqVO);

        // 导出 Excel（按你项目已有的工具）
        ExcelUtils.write(response, "CDR.xls", "数据", ECdrRespVO.class,
                BeanUtils.toBean(list, ECdrRespVO.class));
    }

}